package stringutil

import (
	"crypto/rand"
	"encoding/hex"
	rand2 "math/rand"
	"net/url"
	"strings"
	"time"

	"github.com/bwmarrin/snowflake"

	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/constutil"
)

func GeneratorState() (string, error) {
	b := make([]byte, 16)
	_, err := rand.Read(b)
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(b), nil
}

func GeneratorPassword(l int) string {
	b := []byte(constutil.Letters)
	r := rand2.New(rand2.NewSource(time.Now().UnixNano()))
	var result []byte
	for i := 0; i < l; i++ {
		result = append(result, b[r.Intn(len(b))])
	}
	return string(result)
}

func GeneratorUrl(u url.URL, api string, params url.Values) string {
	return u.Scheme + "://" + u.Host + "/" + strings.Trim(api, "/") + "?" + params.Encode()
}

func GeneratorID() (snowflake.ID, error) {
	id, err := snowflake.NewNode(int64(rand2.Intn(1000)))
	if err != nil {
		return 0, err
	}
	return id.Generate(), nil
}

func GeneratorRandNumber(l int) string {
	b := []byte(constutil.NumberLetters)
	r := rand2.New(rand2.NewSource(time.Now().UnixNano()))
	var result []byte
	for i := 0; i < l; i++ {
		result = append(result, b[r.Intn(len(b))])
	}
	return string(result)
}
